package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysStaff;
import com.ruoyi.system.domain.vo.QyWxMessage;
import com.ruoyi.system.service.ISysStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractMapper;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.service.ISysContractService;

/**
 * 合同Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-14
 */
@Service
public class SysContractServiceImpl extends ServiceImpl<SysContractMapper, SysContract> implements ISysContractService {
    @Autowired
    private SysContractMapper sysContractMapper;
    @Autowired
    private ISysStaffService sysStaffService;

    /**
     * 查询合同
     *
     * @param contractId 合同主键
     * @return 合同
     */
    @Override
    public SysContract selectSysContractByContractId(Long contractId) {
        return sysContractMapper.selectSysContractByContractId(contractId);
    }

    /**
     * 查询合同列表
     *
     * @param sysContract 合同
     * @return 合同
     */
    @Override
    public List<SysContract> selectSysContractList(SysContract sysContract) {
        List<SysContract> sysContracts = sysContractMapper.selectSysContractList(sysContract);
        if (CollUtil.isNotEmpty(sysContracts)) {
            Set<Long> staffIdSet = sysContracts.stream().map(SysContract::getStaffId).collect(Collectors.toSet());
            Map<Long, String> map = SimpleQuery.map(Wrappers.<SysStaff>lambdaQuery().in(SysStaff::getStaffId, staffIdSet), SysStaff::getStaffId, SysStaff::getName);
            for (SysContract contract : sysContracts) {
                contract.setStaffName(map.get(contract.getStaffId()));
            }
        }
        return sysContracts;
    }

    /**
     * 新增合同
     *
     * @param sysContract 合同
     * @return 结果
     */
    @Override
    public int insertSysContract(SysContract sysContract) {
        sysContract.setCreateTime(DateUtils.getNowDate());
        return sysContractMapper.insertSysContract(sysContract);
    }

    /**
     * 修改合同
     *
     * @param sysContract 合同
     * @return 结果
     */
    @Override
    public int updateSysContract(SysContract sysContract) {
        sysContract.setUpdateTime(DateUtils.getNowDate());
        return sysContractMapper.updateSysContract(sysContract);
    }

    /**
     * 批量删除合同
     *
     * @param contractIds 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteSysContractByContractIds(Long[] contractIds) {
        return sysContractMapper.deleteSysContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息
     *
     * @param contractId 合同主键
     * @return 结果
     */
    @Override
    public int deleteSysContractByContractId(Long contractId) {
        return sysContractMapper.deleteSysContractByContractId(contractId);
    }

    @Override
    public List<HashMap<String, Integer>> getContractStatusCounts() {

        return this.sysContractMapper.getContractStatusCounts();
    }


}
